package com.tree;

import java.util.*;

public class Test3 {
    public static void main(String[] args) {

    }
    //判断两个树是否相等
    public boolean isSameTree(TreeNode p, TreeNode q) {


        if (p==null&&q==null){
            return true;
        }
        if (p==null||q==null){
            return false;
        }
        if (p.val!=q.val){
            return false;
        }
        return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);

    }
    //判断二叉树是否对称
    public boolean isSymmetric(TreeNode root) {
        Boolean flag;
        if (root.val!=root.val){
            return false;
        }
        if (root.left==null||root.right==null){
            return false;
        }
        if (root.left==null&&root.right==null){
            return true;
        }
        return isSymmetric(root.left)&&isSymmetric(root.right);
    }
    public boolean isblance(TreeNode root){
        if (root==null){
            return true;
        }else{
            return Math.abs(getWidth(root.left)-getWidth(root.right))  <=1 &&isblance(root.left)&&isblance(root.right);
        }
    }
    public int getWidth(TreeNode root){
        if (root==null){
            return 0;
        }
        return Math.max(getWidth(root.left),getWidth(root.right))+1;
    }
    //层次遍历二叉树
    public List<List<Integer>> levelOrder(TreeNode root){
        ArrayList<List<Integer>> lists = new ArrayList<>();
        if (root==null){
            return lists;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            ArrayList<Integer> list = new ArrayList<>();
            int size = queue.size();
            for (int i = 1; i <=size; i++) {
                TreeNode poll = queue.poll();
                list.add(poll.val);
                if (poll.left!=null){
                    queue.add(poll.left);
                }
                if (poll.right!=null){
                    queue.add(poll.right);
                }
            }
            lists.add(list);
        }
        return lists;

    }
    //二叉树最前浅深度
    public int minDepth(TreeNode root) {

        if (root==null){
            return 0;
        }
        if (root.left==null&&root.right==null){
            return 1;
        }
        int min=Integer.MAX_VALUE;
        if (root.left!=null){
            min=Math.min(minDepth(root.left),min);
        }
        if (root.right!=null){
            min=Math.min(minDepth(root.right),min);
        }
        return min+1;

    }
    public int minDepth2(TreeNode root){
        if (root==null){
            return 0;
        }
        Queue<TreeNode> nodes = new LinkedList<>();
        int level=1;
        nodes.add(root);
        while (!nodes.isEmpty()){
            int size=nodes.size();
            for (int i = 0; i < i; i++) {
                TreeNode treeNode = nodes.poll();
                if (treeNode.left==null&&treeNode.right==null){
                    return level;
                }
                if (treeNode.left!=null){
                    nodes.add(treeNode.left);
                }
                if (treeNode.right!=null){
                    nodes.add(treeNode.right);
                }
                level++;
            }

        }
return level;
    }
    //二叉搜索树 第k个最小第的树
    int count;
    int target;
    public int kthSmallest(TreeNode root, int k) {
        count=k;
        inorder(root);
        return target;

    }
    public void inorder(TreeNode root){
        if (root==null){
            return;
        }
        inorder(root.left);
        if (--count==0){
            target=root.val;
            return;
        }
        inorder(root.right);
    }
    //二叉树最宽宽度
    public int widthOfBinaryTree(TreeNode root) {
        if (root==null){
            return 0;
        }
        //使用双端队列
        Deque<TreeNode> queue = new LinkedList<>();

        queue.offer(root);
        root.val=1;
        int maxw=1;
        while (!queue.isEmpty()){
            int size = queue.size();
            int left = queue.peekFirst().val;
            int right = queue.peekLast().val;
            maxw=Math.max(maxw,right-left+1);

            for (int i = 0; i < size; i++) {
                TreeNode node = queue.peek();
                int val = node.val;
                if (node.left!=null){
                    node.left.val=val*2;
                    queue.offer(node.left);
                }
                if (node.right!=null){
                    node.right.val=val*2+1;
                    queue.offer(node.right);
                }
            }
        }
        return maxw;

    }

        public TreeNode searchBST(TreeNode root, int val) {
        if (root==null){
            return null;
        }
        if (root.val==val){
            return root;
        }
            return searchBST(val < root.val ? root.left : root.right, val);

        }

}
